/*
 * app_bl0937.h
 *
 *  Created on: 2023年9月18日
 *      Author: zlw
 */

#ifndef APP_BL0937_H_
#define APP_BL0937_H_

#include "device_type.h"

#if defined(SM_SMART_PLUG)

#include "bsp_gpio.h"

#define MEASURE_INTERVAL        (32768/2) //500ms
#define ENERGY_SAVE_INTERVAL    (12*60*32768)//10min

typedef enum{
  SEL_CURRENT,
  SEL_VOLTAGE
}selState_t;

typedef union{
  uint32_t voltageCnt;
  uint32_t currentCnt;
}vol_curr_t;

typedef struct{
  vol_curr_t volCurrCnt;
  uint32_t powerCnt;
}bl0937Cnt_t;

typedef struct{
  uint8_t vOverCnt:4;
  uint8_t vUnderCnt:4;
  uint8_t iOverCnt:4;
  uint8_t pOverCnt:4;
  uint8_t vNormal;
}overCnt_t;

typedef struct{
  uint32_t startCnt;
  uint32_t endCnt;
}timeCnt_t;

typedef struct{
  uint32_t current;
  uint32_t voltage;
  uint32_t power;
}energyFre_t;

#define SLIDE_WINDOW_SIZE   6

typedef struct{
  uint16_t data[SLIDE_WINDOW_SIZE];
  uint8_t dataCnt;
  uint8_t index;
} slideWindow_t;

#ifdef SM_SMART_PLUG_ITA
#define BL0937_SEL_PORT     (gpioPortC)
#define BL0937_SEL_PIN      (2U)
#define BL0937_CF_PORT      (gpioPortA)
#define BL0937_CF_PIN       (5U)
#define BL0937_CF1_PORT     (gpioPortA)
#define BL0937_CF1_PIN      (6U)
#else
#define BL0937_SEL_PORT     (gpioPortD)
#define BL0937_SEL_PIN      (3U)
#define BL0937_CF_PORT      (gpioPortC)
#define BL0937_CF_PIN       (0U)
#define BL0937_CF1_PORT     (gpioPortC)
#define BL0937_CF1_PIN      (1U)
#endif

typedef enum{
  MEASURE_NONE,
  MEASURE_CALBRATION,
  MEASURE_NORMAL
}measure_t;

typedef struct{
  uint8_t currentCalibration;       //88
  uint8_t voltageCalibration;       //120
  uint8_t powerCalibration;         //103
}calibration_t;

#define VOLTAGE_MULTI       (10000)
#define CURRENT_MULTI       (100)
#define POWER_MULTI         (1000)

#ifdef SM_SMART_PLUG_US
#define CALIBRATION_VOLTAGE (120 * VOLTAGE_MULTI)   //v
#define CALIBRATION_CURRENT (833 * CURRENT_MULTI) //mA
#define CALIBRATION_POWER   (100 * POWER_MULTI)   //w
#else
#define CALIBRATION_VOLTAGE (220 * VOLTAGE_MULTI)   //v
#define CALIBRATION_CURRENT (392 * CURRENT_MULTI) //mA
#define CALIBRATION_POWER   (86 * POWER_MULTI)   //w
#endif

#define ENERGY_OVERLOAD_CNT     (5)

/*******************overload define***********************/
#ifdef SM_SMART_PLUG_US
#define CURRENT_OVERLOAD (15000)
#define VOLTAGE_OVERLOAD (132)
#define VOLTAGE_UNDER    (92)
#define POWER_OVERLOAD   (18000)
#define VOLTAGE_OVERLOAD_RECOVER    (127)
#define VOLTAGE_UNDER_RECOVER       (102)
#endif
#ifdef SM_SMART_PLUG_EU
#define CURRENT_OVERLOAD (16000)
#define VOLTAGE_OVERLOAD (253)
#define VOLTAGE_UNDER    (184)
#define POWER_OVERLOAD   (36800)
#define VOLTAGE_OVERLOAD_RECOVER    (240)
#define VOLTAGE_UNDER_RECOVER       (195)
#endif
#ifdef SM_SMART_PLUG_ITA
#define CURRENT_OVERLOAD (16000)
#define VOLTAGE_OVERLOAD (253)
#define VOLTAGE_UNDER    (184)
#define POWER_OVERLOAD   (36800)
#define VOLTAGE_OVERLOAD_RECOVER    (240)
#define VOLTAGE_UNDER_RECOVER       (195)
#endif
#ifdef SM_SMART_PLUG_UK
#define CURRENT_OVERLOAD (13000)
#define VOLTAGE_OVERLOAD (253)
#define VOLTAGE_UNDER    (184)
#define POWER_OVERLOAD   (32500)
#define VOLTAGE_OVERLOAD_RECOVER    (240)
#define VOLTAGE_UNDER_RECOVER       (195)
#endif

typedef enum{
  currentOverload,
  currentNormal,
  powerOverload,
  powerNormal,
  voltageOverload,
  voltageUnderload,
  voltageNomal,
  energyStateNum,
}energyState_t;

typedef void (*energyStateCallback)(uint16_t);

/******************calibration define***************************/
#ifdef SM_SMART_PLUG_US
#define CURRNET_CALI_MIN    (600)
#define CURRNET_CALI_MAX    (850)

#define VOLTAGE_CALI_MIN    (8000)
#define VOLTAGE_CALI_MAX    (10500)

#define POWER_CALI_MIN      (600)
#define POWER_CALI_MAX      (800)
#endif
#ifdef SM_SMART_PLUG_EU
#define CURRNET_CALI_MIN    (350)
#define CURRNET_CALI_MAX    (550)

#define VOLTAGE_CALI_MIN    (16000)
#define VOLTAGE_CALI_MAX    (19000)

#define POWER_CALI_MIN      (600)
#define POWER_CALI_MAX      (900)
#endif
#ifdef SM_SMART_PLUG_ITA
#define CURRNET_CALI_MIN    (5)
#define CURRNET_CALI_MAX    (30)

#define VOLTAGE_CALI_MIN    (700)
#define VOLTAGE_CALI_MAX    (900)

#define POWER_CALI_MIN      (10)
#define POWER_CALI_MAX      (60)
#endif
#ifdef SM_SMART_PLUG_UK
#define CURRNET_CALI_MIN    (200)
#define CURRNET_CALI_MAX    (450)

#define VOLTAGE_CALI_MIN    (15500)
#define VOLTAGE_CALI_MAX    (19000)

#define POWER_CALI_MIN      (450)
#define POWER_CALI_MAX      (750)
#endif

void resetCalibration(void);
bool getReadEnergyState(void);
void readEnergyHandler(void);
void energyStateCallbackRegister(energyState_t state, energyStateCallback callback);
void bl0937_init(void);
measure_t measure_state_get(void);
uint16_t currentGet(void);
uint8_t voltageGet(void);
uint16_t powerGet(void);
uint32_t summaEnergyGet(void);

void calibration_finist_callback(void);

#endif

#endif /* _APP_BL0937_H_ */
